<template>
	<view>
		<z-paging ref="paging" v-model="list" @query="queryList" class="container" show-refresher-when-reload>
			<uni-swipe-action ref="swipe_action">
				<uni-swipe-action-item :right-options="isRoleAdmin() ? options : ''" v-for="(item,index) in list"
					:key="item.id" class="swipe_item_container" @click="onClick($event, item)">
					<view class="content" @click="storehouse_click(item)">
						<text class="title">{{item.title}}</text>
					</view>
				</uni-swipe-action-item>
			</uni-swipe-action>
		</z-paging>
		<uni-fab horizontal="right" @fabClick="fabClick" v-if="isRoleAdmin()"></uni-fab>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'

	import {
		onLoad
	} from '@dcloudio/uni-app'

	import {
		isRoleAdmin
	} from '@/utils/role.js'

	const paging = ref(null)

	const type = ref(null)

	const list = ref([])
	const questionsObj = uniCloud.importObject('questionsObj', {
		customUI: true
	})
	const swipe_action = ref(null)

	onLoad((params) => {
		type.value = Number(params.type)
		uni.setNavigationBarTitle({
			title: type.value == 0 ? '模拟考试' : '历年真题'
		})
	})

	const options = ref([{
			text: "编辑",
			style: {
				backgroundColor: "#10ae18",
				fontSize: '13px'
			}
		},
		{
			text: "题目列表",
			style: {
				backgroundColor: "#3396FB",
				fontSize: '13px'
			}
		},
		{
			text: "删除",
			style: {
				backgroundColor: '#E40C39',
				fontSize: '13px'
			}
		}
	])

	const queryList = async (pageNo, pageSize) => {
		let res = await questionsObj.storehouse(pageNo, pageSize, type.value)
		console.log(type.value == 0 ? '模拟考试' : '历年真题', res);
		if (res.errCode != 0) {
			paging.value.complete(false)
			return
		}
		paging.value.complete(res.data)
	}

	function onClick(e, item) {
		if (e.index == 0) {
			// 编辑
			storehouse_edit(item)
		} else if (e.index == 1) {
			// 列表
			storehouse_topics(item)
		} else if (e.index == 2) {
			// 删除
			storehouse_delete_action(item)
		}
		swipe_action.value.closeAll()
	}

	function storehouse_click(params) {
		uni.navigateTo({
			url: '/pages_lianxi/linianzhenti/topic_practice?question_storehouse_id=' + params._id + '&type=' + type
				.value
		})
	}

	function storehouse_topics(params) {
		uni.navigateTo({
			url: '/pages_lianxi/linianzhenti/topic?question_storehouse_id=' + params._id + '&title=' + params.title
		})
	}

	function storehouse_edit(params) {
		uni.navigateTo({
			url: '/pages_lianxi/linianzhenti/storehouse_publish?id=' + params._id + '&type=' + type
				.value
		})
	}

	/* 删除事件 */
	function storehouse_delete_action(params) {
		uni.showModal({
			title: '温馨提醒',
			content: `确定删除"${params.title}"，删除题库将同步删除该题库下的题目?`,
			success: (res) => {
				if (!res.confirm) {
					return
				}
				storehouse_delete(params)
			}
		})
	}

	/* 删除 */
	async function storehouse_delete(params) {
		uni.showLoading()
		let result = await questionsObj.storehouse_delete(params._id)
		uni.hideLoading()
		let flag = result.errCode == 0
		uni.showToast({
			title: flag ? "删除成功" : result.errMsg,
			icon: flag ? 'success' : 'fail'
		})
		if (flag) {
			paging.value.refresh()
		}
	}

	function fabClick() {
		uni.navigateTo({
			url: '/pages_lianxi/linianzhenti/storehouse_publish?type=' + type.value
		})
	}

	uni.$on('storehouse_publish', () => {
		if (paging.value) {
			paging.value.refresh()
		}
	})
</script>

<style lang="scss">
	page {
		background-color: white;
	}
</style>

<style lang="scss" scoped>
	.container {
		background-color: white;

		.swipe_item_container {
			margin-top: 20rpx;

			.content {
				margin: 0 20rpx;
				padding: 26rpx 20rpx;
				background-color: $me-color-background;
				border-radius: 8rpx;

				.title {
					font-size: 28rpx;
					color: $me-color-gray-1;
				}
			}
		}
	}
</style>